/**
 * Project:   dtu.fmes.googleloon
 * File:      RunSimulation.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright:	Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * 				Nicolai Sonne [ NicolaiSonne@hotmail.com.com ]
 * Date:      Dec 11, 2013
 */

package dtu.fmes.loon;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

import dtu.fmes.loon.exporter.Exporter;
import dtu.fmes.loon.exporter.JsonExporter;
import dtu.fmes.loon.importer.Importer;
import dtu.fmes.loon.importer.JsonImporter;
import dtu.fmes.loon.simulation.SimulationExecutor;
import dtu.fmes.loon.simulation.processors.AdjustHeightSimulationLink;
import dtu.fmes.loon.simulation.processors.ChargeBatterySimulationLink;
import dtu.fmes.loon.simulation.processors.ConstantPowerDrawSimulationLink;
import dtu.fmes.loon.simulation.processors.FindTargetHeightSimulationLink;
import dtu.fmes.loon.simulation.processors.KillDischargedBalloonsSimulationLink;
import dtu.fmes.loon.simulation.processors.KillRandomBalloonSimulationLink;
import dtu.fmes.loon.simulation.processors.SimulationLink;
import dtu.fmes.loon.simulation.processors.WindSimulationLink;
import dtu.fmes.loon.singletons.CurrentSimulationStateSingleton;

/**
 * Main class for running simulation
 * 
 * @author NicolaiS
 * 
 */
public class RunSimulation {

	private static final Logger log = Logger.getLogger(RunSimulation.class);

	public static void main(String[] args) throws Exception {

		// Load input file

		if (args.length <= 0) {
			System.err.println("Please specify input file!");
			log.error("Please specify input file!");
			System.exit(-1);
		}

		String inputFile = args[0];

		@SuppressWarnings("unused")
		CurrentSimulationStateSingleton simulationState = CurrentSimulationStateSingleton.instance();

		// Create importer and import balloon data
		Importer importer = new JsonImporter();
		importer.doImport(inputFile);

		// Create simulation pipeline
		Collection<SimulationLink> simulationLinks = new ArrayList<SimulationLink>();
		simulationLinks.add(new KillRandomBalloonSimulationLink());
		simulationLinks.add(new ChargeBatterySimulationLink());
		simulationLinks.add(new FindTargetHeightSimulationLink());
		simulationLinks.add(new AdjustHeightSimulationLink());
		simulationLinks.add(new WindSimulationLink());
		simulationLinks.add(new ConstantPowerDrawSimulationLink());
		simulationLinks.add(new KillDischargedBalloonsSimulationLink());

		// Run simulation
		SimulationExecutor executor = new SimulationExecutor(simulationLinks);
		executor.runSimulation();

		if (log.isInfoEnabled()) {
			log.info("Writing file......");
		}

		// Export simulation result to a file
		Exporter exporter = new JsonExporter();
		exporter.doExport(new File(inputFile + ".result"));

		if (log.isInfoEnabled()) {
			log.info("Finished writing file!");
		}
	}

}
